<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Tasks  {

	public function display() {
		require_once('google-api-php-client/src/google_client.php');
		$this->Google_Client = new Google_client();
		$this->Google_Client->setApplicationName('Google+ PHP Starter Application');
	// Visit https://code.google.com/apis/console?api=plus to generate your
	// client id, client secret, and to register your redirect uri.
		$this->Google_Client->setClientId('1023179910439.apps.googleusercontent.com');
		$this->Google_Client->setClientSecret('TV_S9i0w5qv2UMLBbE5CBz4n');
		$this->Google_Client->setRedirectUri('http://localhost/test.php');
		$this->Google_Client->setDeveloperKey('AIzaSyCvOCzhelwl3W4Mmlg2rHKvQiq2-yA8yqE');
		require_once('google-api-php-client/src/contrib/Google_TasksService.php');
		$this->tasksService = new Google_TasksService($this->Google_Client);

		if ($this->session->userdata('access_token')) {
			$html = "";

			$this->Google_Client->setAccessToken($this->session->userdata('access_token'));
			
			$lists = $this->tasksService->tasklists->listTasklists();
			$html .= "<ul class='nav nav-list'>\n";
			foreach ($lists['items'] as $list) {

				$task = new Google_Task();
				$task->setTitle("New Task");
				$task->setNotes("Please complete me");

				//$result =  $this->tasksService->tasks->insert($list['id'], $task);

				$html = $html." <li class='nav-header'>{$list['title']}</li> \n";
				$tasks = $this->tasksService->tasks->listTasks($list['id']);
				if(isset($tasks['items']) && count($tasks['items']) > 0){
					//$html.="<ul>";

					foreach($tasks['items'] as $task){
						$html.= "<li><a href='#'>".$task['title']."</a></li>";
					}

					//$html .="</ul>";
				}
			}
			$html .= "</ul>\n";
		} else {
			$html = "No access token";
		}

		return $html;
	}
}
